package db_wms_plugin

import (
	"gorm.io/gorm"
	"time"
	"wms-server/fw/fw_utils"
)

type CreateBeforePlugin struct{}

func (t *CreateBeforePlugin) Name() string {
	return "create_before_plugin"
}

func (t *CreateBeforePlugin) Initialize(db *gorm.DB) error {
	return db.Callback().Create().Before("gorm:create").Register("tenantAndCreatedBy", t.beforeCreate)
}

func (t *CreateBeforePlugin) beforeCreate(db *gorm.DB) {
	//设置ID
	db.Statement.SetColumn("id", fw_utils.GenXid(), true)
	//设置创建时间
	db.Statement.SetColumn("created_at", time.Now(), true)
	////设置创建人
	//tmp := db.Statement.Context.Value("jwtInfo")
	//if tmp != nil { // 不为空
	//	jwtInfo := tmp.(*fw_jwt.CustomClaims)
	//	db.Statement.SetColumn("created_by", jwtInfo.UserId, true)
	//}
}
